
#include "iflytekAsrRequestParam.h"
//const char* iflytekAsrRequestParam::BUSINESSARGS="{\"domain\": \"iat\", \"language\": \"zh_cn\", \"accent\": \"mandarin\", \"vinfo\":0,\"vad_eos\":10000,\"dwa\":\"wpgs\"}";
const char* iflytekAsrRequestParam::BUSINESSARGS="{\"domain\": \"iat\", \"language\": \"zh_cn\", \"accent\": \"mandarin\", \"vinfo\":0,\"vad_eos\":10000}";
const char* iflytekAsrRequestParam::RFC1123FORMAT="%3s, %02d %3s %4d %02d:%02d:%02d GMT";
const uint8_t iflytekAsrRequestParam::alphabet_map[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const char *const iflytekAsrRequestParam::rfc1123_weekdays[7] = { 
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" 
};
const char *const iflytekAsrRequestParam::short_months[12] = { 
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
std::string iflytekAsrRequestParam::createUrl()
{
	const char* url = "ws://ws-api.xfyun.cn/v2/iat";
	time_t timep;
	time(&timep);
	char* rfc1123T = getRFC1123Time(timep);
	char signature_origin[1024]={'\0'};
	sprintf(signature_origin,"host: ws-api.xfyun.cn\ndate: %s\nGET /v2/iat HTTP/1.1",rfc1123T);
	printf("Get signature_origin %s\n",signature_origin);
	uint8_t *key;
	uint8_t *msg;
	key = reinterpret_cast< uint8_t *>(const_cast<char*>(APISECRET));
	msg = reinterpret_cast< uint8_t *>(&signature_origin[0]);
	genEncodeStr(key,strlen(APISECRET),msg,strlen(signature_origin));
	char authorization_origin[1024]={'\0'};
	sprintf(authorization_origin,"api_key=\"%s\", algorithm=\"%s\", headers=\"%s\", signature=\"%s\"",APIKEY,"hmac-sha256", "host date request-line", _encodeStr);	 	
	printf("Get authorization_origin %s\n",authorization_origin);
	uint8_t authMd[1024];
	
	uint32_t iSize = base64_encodeStr(authorization_origin,strlen(authorization_origin),authMd);
	char finalAuth[1024]={'\0'};	
	for(uint32_t i = 0; i < iSize; i++)
		sprintf(finalAuth +i,"%c",authMd[i]);
	//char finalUrlAppend[1024]={'\0'};
	//sprintf(finalUrlAppend,"authorization=%s&date=%s&host=ws-api.xfyun.cn",finalAuth,rfc1123T);	
	_finalUrl = std::string(url) + std::string("?date=") + urlEncode(rfc1123T)+"&host=ws-api.xfyun.cn&authorization=" + urlEncode(finalAuth);	
	return _finalUrl;
}
